<template>
	<view class="wallet-layout">
		<!-- 导航 -->
		<view class="nav-box">
			<view :style="{height:safeTop + 'px'}"></view>
			<view class="nav-bar">
				<text class="nav-title">钱包与账单</text>
				<image class="nav-icon" :src="channelStore.channelTypeRedIcon" mode="aspectFit"></image>
			</view>
		</view>
		<view class="wallet-container" :style="{marginTop:safeTop + 44 + 'px'}">
			<!-- 钱包数据 -->
			<view class="wallet-info" :class="[`wallet-info-bg-${channelStore.channelType}`]">
				<text>账户余额(元)</text>
				<text class="wallet-info-money">{{dataInfo.balance}}</text>
				<view class="wallet-info-bottom">
					<view class="wallet-info-bottom-item">
						<text class="wallet-info-bottom-item-money xh-lines-1">{{dataInfo.withdrawalAmount}}</text>
						<text>可提现金额</text>
					</view>
					<view class="wallet-info-bottom-item">
						<text class="wallet-info-bottom-item-money xh-lines-1">{{dataInfo.frozenAmount}}</text>
						<text>冻结金额</text>
					</view>
					<view class="wallet-info-bottom-item">
						<text class="wallet-info-bottom-item-money xh-lines-1">{{dataInfo.pendingRevenueAmount}}</text>
						<text>待入账金额</text>
					</view>
				</view>
				<view class="wallet-info-tx" @tap="showWalletList">
					<image class="wallet-info-tx-icon" src="/static/wallet/wallet_withdrawal@2x.png" mode="aspectFit"></image>
					<text>提现</text>
				</view>
			</view>
			<!-- 菜单 -->
			<view class="wallet-menu">
				<view class="wallet-menu-item" v-for="item in menuList" :key="item.type" @tap="didSelectMenuItem(item)">
					<image class="wallet-menu-item-icon" :src="item.icon" mode="aspectFit"></image>
					<text>{{item.title}}</text>
				</view>
			</view>
			<!-- 提现明细和收益明细菜单 -->
			<view class="wallet-record-menu">
				<view class="wallet-record-menu-item" v-for="item in channelStore.walletMenuList" :key="item.type" @tap="didSelectMenuItem(item)">
					<image class="wallet-record-menu-item-icon" :src="item.icon" mode="aspectFit"></image>
					<text>{{item.title}}</text>
				</view>
			</view>
			<!-- 广告 -->
			<view class="banner-box">
				<swiper class="swiper" :indicator-dots="true" :autoplay="true" :circular="true" :interval="3000" :duration="500">
					<swiper-item class="swiper-item" v-for="(item,index) in bannerList" :key="index" @tap="didSelectBannerItem(item)">
						<image class="swiper-image" :src="item.imgPath" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="bottom-tip">
				<text>每天努力一点,离成功更近一点</text>
			</view>
		</view>
		<xh-auth-pop ref="authRef"></xh-auth-pop>
	</view>
</template>

<script setup>
	import { onLoad ,onShow,onPullDownRefresh } from '@dcloudio/uni-app'
	import { ref,nextTick} from 'vue'
	import { useChannelStore,useUserStore } from '@/common/stores'
	import { getBannerList , getWalletsInfo} from '@/common/api/api.js'
	const userStore = useUserStore()
	const channelStore = useChannelStore()
	const safeTop = ref(0)
	const authRef = ref(null)
	const bannerList = ref([])
	const dataInfo = ref({
		balance:0,
		pendingRevenueAmount:0,
		frozenAmount:0,
		withdrawalAmount:0
	})
	const menuList = ref([{
			title: '钱包',
			icon: '/static/wallet/wallet_menu1@2x.png',
			type: 1,
		},
		{
			title: '账单',
			icon: '/static/wallet/wallet_menu2@2x.png',
			type: 2
		},
		{
			title: '结算卡',
			icon: '/static/wallet/wallet_menu3@2x.png',
			type: 3
		}
	])
	// 选择菜单
	const didSelectMenuItem = (item)=>{
		if(!userStore.isAuth) {
			authRef.value.open()
			return
		}
		if(item.type == 1){
			// 钱包
			showWalletList()
		}
		else if(item.type == 2){
			// 账单
			uni.navigateTo({
				url:'/pages/wallet/bills/bills'
			})
		}
		else if(item.type == 3){
			uni.navigateTo({
				url:'/pages/wallet/bankcard-list/bankcard-list'
			})
		}
		else if(item.type == 4){
			// 提现明细
			uni.navigateTo({
				url:'/pages/wallet/withdrawal-record/withdrawal-record'
			})
		}
		else{
			// 收益明细
			uni.navigateTo({
				url:'/pages/wallet/income-summary/income-summary'
			})
		}
	}
	// 我的钱包
	const showWalletList = ()=>{
		if(!userStore.isAuth) {
			authRef.value.open()
			return
		}
		uni.navigateTo({
			url:'/pages/wallet/wallet-list/wallet-list'
		})
	}
	// 获取banner数据
	const getBannerData = async ()=>{
		const res = await getBannerList({location:'03'})
		bannerList.value = res.result
	}
	// 获取钱包数据
	const getWalletsData = async ()=>{
		const res = await getWalletsInfo()
		dataInfo.value.withdrawalAmount = res.result.withdrawalAmount
		dataInfo.value.frozenAmount = res.result.frozenAmount
		dataInfo.value.pendingRevenueAmount = res.result.pendingRevenueAmount
		dataInfo.value.balance = dataInfo.value.withdrawalAmount + dataInfo.value.frozenAmount + dataInfo.value.pendingRevenueAmount
	}
	//点击banner
	const didSelectBannerItem = (item)=>{
		if(!userStore.isAuth){
			authRef.value.open()
			return
		}
		if(item.link && item.link.startsWith('http')){
			uni.navigateTo({
				url:`/pages/webview/webview?url=${item.link}`
			})
		}
	}
	//刷新数据
	const reloadData = ()=>{
		getWalletsData()
		getBannerData()
	}
	onShow(()=>{
		userStore.updateUserInfo()
		reloadData()
	})
	
	onLoad( () => {
		safeTop.value = uni.getSystemInfoSync().safeAreaInsets.top
		channelStore.updateTabBarStyle()
		nextTick(()=>{
			if(!userStore.isAuth) {
				authRef.value.open()
			}
		})
		reloadData()
	})
</script>

<style lang="scss" scoped>
	.wallet-layout {
		background-color: white;
		min-height: 100vh;
		padding-top: 20rpx;
		.nav-box {
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			background-color: white;
			z-index: 99999;

			.nav-bar {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 44px;
				position: relative;
				font-size: 36rpx;
				.nav-icon {
					width: 128rpx;
					height: 56rpx;
					position: absolute;
					right: 16rpx;
				}
			}
		}

		.wallet-container {
			padding: 0 30rpx;

			.wallet-info-bg-KDB {
				background-image: url('/static/wallet/wallet_top_bg_KDB@2x.png');
			}

			.wallet-info-bg-QB {
				background-image: url('/static/wallet/wallet_top_bg_QB@2x.png');
			}

			.wallet-info-bg-HHB {
				background-image: url('/static/wallet/wallet_top_bg_HHB@2x.png');
			}

			.wallet-info {
				background-size: 100% 100%;
				background-repeat: no-repeat;
				color: #ffffff;
				padding: 30rpx;
				position: relative;
				font-size: 30rpx;
				display: flex;
				flex-direction: column;

				.wallet-info-tx {
					position: absolute;
					display: flex;
					align-items: center;
					color: white;
					right: 30rpx;
					padding: 0 20rpx;
					height: 52rpx;
					border: 1px solid #ffffff;
					border-radius: 26rpx;

					.wallet-info-tx-icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 10rpx;
					}
				}

				.wallet-info-money {
					font-size: 64rpx;
					margin-top: 10rpx;
				}

				.wallet-info-bottom {
					display: flex;
					align-items: center;
					margin-top: 30rpx;

					.wallet-info-bottom-item {
						display: flex;
						flex-direction: column;
						align-items: center;
						flex: 1;
						color: #ffffff;
						font-size: 26rpx;

						.wallet-info-bottom-item-money {
							font-size: 38rpx;
							font-weight: 500;
							margin-bottom: 10rpx;
						}
					}
				}

			}

			.wallet-menu {
				display: grid;
				grid-template-columns: repeat(3, 1fr);
				padding: 40rpx 0;
				.wallet-menu-item {
					display: flex;
					flex-direction: column;
					align-items: center;

					.wallet-menu-item-icon {
						width: 80rpx;
						height: 80rpx;
					}
				}
			}
			.wallet-record-menu{
				display: grid;
				grid-template-columns: repeat(2, 1fr);
				padding: 30rpx 0;
				gap: 20rpx;
				.wallet-record-menu-item{
					display: flex;
					align-items: center;
					justify-content: center;
					border: 1px solid $xh-line-color;
					border-radius: 16rpx;
					color: $xh-color-3;
					padding: 10rpx 0;
					.wallet-record-menu-item-icon{
						width: 80rpx;
						height: 80rpx;
						margin-right: 30rpx;
					}
				}
			}
			
			.swiper {
				width: 100%;
				height: 200rpx;
				border-radius: 20rpx;
				overflow: hidden;
				.swiper-image {
					width: 100%;
					height: 100%;
				}
			}
			.bottom-tip{
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 40rpx;
				color: #C3C3C3;
				font-size: 26rpx;
			}
		}
	}
</style>